import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { getConnection, Repository } from 'typeorm';
import { createZongjuDataDto, LianluDataDto } from './dto/zongju.dto';
import { Zongju } from './zongju.entity';

@Injectable()
export class ZongjuService {
  constructor(
    @InjectRepository(Zongju)
    private zongjuRepository: Repository<Zongju>,
  ) {}

  // 获取总局链路信息
  async getZongjuList() {
    const result = await getConnection()
      .createQueryBuilder()
      .select() // 这里添加参数会将每一个返回的字段的名称前面都添加一个job_
      .from(Zongju, 'zongju')
      .execute(); // 将获取的数据类型转换为json类型
    return result;
  }

  // 根据链路编号查找链路
  async getZongjuLianluInfoByCode(
    lianlu_code: string,
  ): Promise<LianluDataDto[]> {
    const result = await getConnection()
      .createQueryBuilder()
      .select()
      .from(Zongju, 'zongju')
      .where('zongju.lianlu_code = :lianlu_code', { lianlu_code })
      .execute();
    return result;
  }

  // 根据链路id查找链路
  async getZongjuLianluInfoById(id: string) {
    const result = await getConnection()
      .createQueryBuilder()
      .select()
      .from(Zongju, 'zongju')
      .where('zongju.id = :id', { id })
      .execute();
    return result;
  }

  // 新建总局链路信息
  async createZongju(values: Array<createZongjuDataDto>) {
    const result = await getConnection()
      .createQueryBuilder()
      .insert()
      .into(Zongju)
      .values(values)
      .execute();
    return result;
  }

  // 根据id删除链路信息
  async delLianlu(id: string) {
    const result = await getConnection()
      .createQueryBuilder()
      .delete()
      .from(Zongju)
      .where('id = :id', { id })
      .execute();
    return result;
  }

  // 修改链路信息
  async updataLianlu(id: string, data) {
    const result = await getConnection()
      .createQueryBuilder()
      .update(Zongju)
      .set(data)
      .where('id = :id', { id })
      .execute();
    return result;
  }
}
